﻿using System;
using System.Collections.Generic;

using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.RelationQuery
{
    public partial class RelationToCheck : BasePage
    {
        public RelationToCheck()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
            this.PageOperateType = OperateType.配送关系检查.ToString();
        }
        IBLL_Company IBC = Factory.BLL_Company();
        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();
        IBLL_ProcurementBasic iDistribution = Factory.BLL_ProcurementBasic();
        //存放地区编号名称信息的字典
        Dictionary<string, string> dicRule = new Dictionary<string, string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
            }
        }
        /// <summary>
        /// 获取未配送的地区
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        protected string GetUnDisAreaNames(string strIDs)
        {
            string strAreaNames = string.Empty;
            foreach (string AreaID in dicRule.Keys)
            {
                if (strIDs.Contains(AreaID) == false)
                {
                    strAreaNames += dicRule[AreaID] + "、";
                }
            }
            return strAreaNames;
        }
        /// <summary>
        /// 检查确认情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnCheckConfirm_Click(object sender, EventArgs e)
        {
            Bind();
        }

        public void Bind()
        {
            DataTable dtAll = getDataInfo();
            this.grvConfirmList.DataSource = dtAll;
            this.grvConfirmList.DataBind();
        }
        protected void hzbtnExcelImport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.SortFields = " companyid_TB,ProcureCataLogID ";

            //int totalrecord = 0;
            //DataTable dtAll = iDistribution.GetProcureCatalogDTByWhere(procureCatalog, 1, 0, out totalrecord);

            //HZ.Excel.ExcelHelper.ExportDataForWeb(dtAll, this.grvConfirmList);
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.ProcureCatalog>(procureCatalog, true);
            string sql = string.Format(@"SELECT ProcureCatalogID 产品编号,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Unit 最小计量单位,PackUnit 包装单位,
                                         CompanyName_SC 生产企业,CompanyName_TB 投标企业 FROM View_ProcureCatalog_tb WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "配送关系检查.xls");
        }
        protected DataTable getDataInfo()
        {

            #region 代码逻辑和check函数一样
            //获取该企业的中标目录

            HZ.DrugTrade.Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            //procureCatalog.CompanyID_TB = CurrentAccount.GetUser().Org.OrgID;
            procureCatalog.SortFields = " companyid_TB,ProcureCataLogID ";

            int totalrecord = 0;
           

            DataTable dtAll = iDistribution.GetProcureCatalogDTByWhere(procureCatalog,this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
            this.AspNetPager1.RecordCount = totalrecord;
            if (totalrecord > 0)
            {

                lblTotal.Text = "总记录共 " + totalrecord + " 条";

                dtAll.Columns.Add(new DataColumn("AreaNames", Type.GetType("System.String")));

                //获取企业的配送关系
                string strWhere = " ";
                //非拒绝的配送关系
                strWhere += " and status='可配送'  order by companyid_TB,ProcureCatalogID ";

                int totalCount = 0;
                DataTable dt = iselectRelation.GetSelectRelationToConfirm(strWhere, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalCount);
                if (totalrecord <= 0)
                {
                    foreach (DataRow row in dtAll.Rows)
                    {
                        row["AreaNames"] = "全区";
                    }
                }
                else
                {
                    #region 十二盟市信息
                    if (dicRule.Count != 12)
                    {
                        dicRule.Clear();
                        dicRule.Add("150100", "呼和浩特市");
                        dicRule.Add("150200", "包头市");
                        dicRule.Add("150300", "乌海市");
                        dicRule.Add("150400", "赤峰市");
                        dicRule.Add("150500", "通辽市");
                        dicRule.Add("150600", "鄂尔多斯市");
                        dicRule.Add("150700", "呼伦贝尔市");
                        dicRule.Add("150800", "巴彦淖尔市");
                        dicRule.Add("150900", "乌兰察布市");
                        dicRule.Add("152200", "兴安盟");
                        dicRule.Add("152500", "锡林郭勒盟");
                        dicRule.Add("152900", "阿拉善盟");
                    }
                    #endregion


                    //配送关系检查
                    for (int _index = 0; _index < dtAll.Rows.Count; _index++)
                    {
                        string strProcureCatalogID = dtAll.Rows[_index]["ProcureCatalogID"].ToString();

                        DataRow[] dataRows = dt.Select("ProcureCatalogID='" + strProcureCatalogID + "'", "AreaID");

                        if (dataRows.Length == 0)
                        {
                            dtAll.Rows[_index]["AreaNames"] = "全区";
                        }
                        else
                        {
                            string strAreaNames = string.Empty;

                            foreach (DataRow dr in dataRows)
                            {
                                strAreaNames += dr["AreaID"].ToString() + ",";
                            }

                            dtAll.Rows[_index]["AreaNames"] = GetUnDisAreaNames(strAreaNames);
                        }
                    }

                    //配送关系检查
                    for (int _index = 0; _index < dtAll.Rows.Count; )
                    {

                        string strAreaNames = dtAll.Rows[_index]["AreaNames"].ToString();
                        if (strAreaNames.Length <= 0)
                        {
                            dtAll.Rows.RemoveAt(_index);
                        }
                        else
                        {
                            _index++;
                        }
                    }
                }
            }
            #endregion

            return dtAll;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }
    }
}
